package com.jrh.web.admin;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jrh.biz.showroom.ShowroomService;
import com.jrh.biz.user.UserService;
import com.jrh.biz.userscheme.UserSchemeService;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Showroom;
import com.jrh.domain.entity.UserScheme;
import com.jrh.domain.entity.User;
import com.jrh.web.admin.form.AdminUserSchemeForm;

/**
 * 类userschemeController.java的实现描述：用户方案管理
 * 
 * @author mmyjse@gmail.com 2012-7-25 上午1:05:42
 */
@Controller
@RequestMapping("/admin/userscheme")
public class AdminUserSchemeController {

	private static final Integer pageSize = 5;

	@Autowired
	private UserSchemeService userschemeService;

	@Autowired
	private ShowroomService showroomService;

	@Autowired
	private UserService userService;

	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String query(Integer pageIndex, Long userid1, Long showroomid1,
			String ispublic1, String schemename1, String designer1,
			Model model, HttpServletRequest req) {
		UserScheme userscheme = new UserScheme();
		userscheme.setUserid(userid1);
		userscheme.setShowroomid(showroomid1);
		userscheme.setIspublic(ispublic1);
		userscheme.setName(schemename1);
		userscheme.setDesigner(designer1);
		if (pageIndex == null) {
			pageIndex = 1;
		}
		Integer start = (pageIndex - 1) * pageSize;
		Page<UserScheme> list = userschemeService.listBrandsByCondition(
				userscheme, start, pageSize);
		model.addAttribute("list", list);
		List<Showroom> showroomList = showroomService.findshowroomid();
		List<User> userList = userService.finduserid();
		model.addAttribute("datas1", showroomList);
		model.addAttribute("datas2", userList);
		model.addAttribute("userid1", userid1);
		model.addAttribute("showroomid1", showroomid1);
		model.addAttribute("ispublic1", ispublic1);
		model.addAttribute("schemename1", schemename1);
		model.addAttribute("designer1", designer1);
		return "admin/userscheme/list";
	}

	@RequestMapping(method = RequestMethod.GET, value = "/update/{id}/{pageIndex}")
	public String queryupdate(String success, @PathVariable Long id,
			@PathVariable Integer pageIndex, Model model, Long userid1,
			Long showroomid1, String ispublic1, String schemename1,
			String designer1) throws UnsupportedEncodingException {
		UserScheme userscheme = userschemeService.loadById(id);
		List<Showroom> showroomList = showroomService.findshowroomid();
		List<User> userList = userService.finduserid();
		model.addAttribute("datas1", showroomList);
		model.addAttribute("datas2", userList);
		model.addAttribute("data3", userscheme);
		model.addAttribute("pageIndex", pageIndex);
		model.addAttribute("success", Boolean.valueOf(success));
		model.addAttribute("userid1", userid1);
		model.addAttribute("showroomid1", showroomid1);
		model.addAttribute("ispublic1", ispublic1);
		model.addAttribute("schemename1", schemename1);
		model.addAttribute("designer1", designer1);
		return "admin/userscheme/update";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/update/{pageIndex}")
	public String doqueryUpdate(
			@ModelAttribute("userscheme") AdminUserSchemeForm userschemeForm,
			@PathVariable Integer pageIndex, Long userid1, Long showroomid1,
			String ispublic1, String schemename1, String designer1,
			HttpServletRequest req) throws UnsupportedEncodingException {
		UserScheme userscheme = new UserScheme(userschemeForm.getId(),
				userschemeForm.getUserid(), userschemeForm.getName(),
				userschemeForm.getDesigner(), userschemeForm.getBudget(),
				userschemeForm.getShowroomid(), userschemeForm.getIspublic());
		boolean isSuccess = userschemeService.updateUserScheme(userscheme);
		if (userid1 == null & showroomid1 == null) {
			return "redirect:/admin/userscheme/update/" + userscheme.getId()
					+ "/" + pageIndex + ".htm?" + "success=" + isSuccess
					+ "&ispublic1=" + java.net.URLEncoder.encode(ispublic1,"UTF-8") + "&schemename1=" + java.net.URLEncoder.encode(schemename1,"UTF-8")
					+ "&designer1=" + java.net.URLEncoder.encode(designer1,"UTF-8");
		} else if (userid1 == null & showroomid1 != null) {
			return "redirect:/admin/userscheme/update/" + userscheme.getId()
					+ "/" + pageIndex + ".htm?" + "success=" + isSuccess
					+ "&showroomid1=" + showroomid1 + "&ispublic1=" + java.net.URLEncoder.encode(ispublic1,"UTF-8")
					+ "&schemename1=" + java.net.URLEncoder.encode(schemename1,"UTF-8") + "&designer1=" + java.net.URLEncoder.encode(designer1,"UTF-8");
		} else if (userid1 != null & showroomid1 == null) {
			return "redirect:/admin/userscheme/update/" + userscheme.getId()
					+ "/" + pageIndex + ".htm?" + "success=" + isSuccess
					+ "&userid1=" + userid1 + "&ispublic1=" + java.net.URLEncoder.encode(ispublic1,"UTF-8")
					+ "&schemename1=" + java.net.URLEncoder.encode(schemename1,"UTF-8") + "&designer1=" + java.net.URLEncoder.encode(designer1,"UTF-8");
		} else {
			return "redirect:/admin/userscheme/update/" + userscheme.getId()
					+ "/" + pageIndex + ".htm?" + "success=" + isSuccess
					+ "&userid1=" + userid1 + "&showroomid1=" + showroomid1
					+ "&ispublic1=" + java.net.URLEncoder.encode(ispublic1,"UTF-8") + "&schemename1=" + java.net.URLEncoder.encode(schemename1,"UTF-8")
					+ "&designer1=" + java.net.URLEncoder.encode(designer1,"UTF-8");
		}
	}

	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public String doAdd(
			@ModelAttribute("userscheme") AdminUserSchemeForm userschemeForm,
			Integer pageIndex) throws ParseException {
		Date date = new Date();
		UserScheme userscheme = new UserScheme(userschemeForm.getId(),
				userschemeForm.getUserid(), userschemeForm.getName(), date,
				userschemeForm.getDesigner(), userschemeForm.getBudget(),
				userschemeForm.getShowroomid(), userschemeForm.getIspublic());
		boolean isSuccess = userschemeService.addUserScheme(userscheme);
		if (isSuccess) {
			return "redirect:/admin/userscheme/list?pageIndex=" + pageIndex;
		} else {
			return "/admin/error";
		}
	}

	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/delete/{id}")
	public String delete(@PathVariable Long id) {
		boolean isSuccess = userschemeService.deleteUserSchemeBy(id);
		if (isSuccess) {
			return Boolean.TRUE.toString();
		} else {
			return Boolean.FALSE.toString();
		}
	}

}
